﻿// -----------------------------------------------------------------------
// <copyright file="Asteroid.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SpaceshipDuel
{
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    public enum AsteroidSize
    {
        big = 0,
        medium = 1,
        small = 2
    }

    /// <summary>
    /// Klasse für Asteroiden
    /// </summary>
    public class Asteroid : GameObject
    {
        #region Private fields

        int radius;             // Radius des Asteroiden (für erste Stufe der Kollisionserkennung)
        int structIntegrity;    // "Lebenspunkte" des Asteroiden, bei 0 wird er zerstört
        float rotationSpeed;    // Rotationsgeschwindigkeit
        float rotationAngle;    // Rotationswinkel 
        bool destroyMe;         // Flag zum Löschen
        AsteroidSize size;      // Größe des Asteroiden
        Vector2 velocity;       // Geschwindigkeitsvektor

        #endregion Private fields

        #region Public properties

        public bool DestroyMe
        {
            get { return this.destroyMe; }
            set { this.destroyMe = value; }
        }

        public int Radius
        {
            get { return this.radius; }
            set { this.radius = value; }
        }

        public AsteroidSize Size
        {
            get { return this.size; }
            set { this.size = value; }
        }

        public float RotationAngle
        {
            get { return this.rotationAngle; }
            set { this.rotationAngle = value; }
        }

        #endregion Public properties

        #region Construction

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="newTexture">Textur des Schusses</param>
        /// <param name="newPosition">Startposition des Schusses</param>
        /// <param name="newWorldSize">Größe des Spielfeldes</param>
        /// <param name="newSpeed">Geschossgeschwindigkeit</param>
        /// <param name="newRotation">aktueller Rotationswinkel des Raumschiffs = Orientierung der Schusstextur</param>
        /// <param name="newDirection">Richtungsvektor für Richtung des Schusses (normiert)</param>
        /// <param name="newMaxTravelDistance">Reichweite des Schusses</param>
        public Asteroid(TextureData newTextureData, Vector2 newCenter, int newWorldSize, AsteroidSize newSize, Random rnd)
        {
            destroyMe = false;
            textureData = newTextureData;
            radius = textureData.halfheight;
            center = newCenter;
            worldSize = newWorldSize;
            rotationAngle = rnd.Next(180);
            rotationSpeed = rnd.Next(10) / 1000f;
            velocity = new Vector2(rnd.Next(10) / 40f, rnd.Next(10) / 40f);
            size = newSize;

            switch (size)
            {
                case AsteroidSize.big:
                    structIntegrity = rnd.Next(40) + 10;
                    break;
                case AsteroidSize.medium:
                    structIntegrity = rnd.Next(20) + 5;
                    break;
                case AsteroidSize.small:
                    structIntegrity = rnd.Next(5) + 1;
                    break;
            }
        }

        #endregion Construction

        #region Public methods

        /// <summary>
        /// Update-Methode
        /// </summary>
        new public void Update()
        {
            if (destroyMe == false)
            {
                center += velocity;
                CheckPosition();

                rotationAngle += rotationSpeed;
                rotationAngle = (float)(rotationAngle % (Math.PI * 2));

                if (structIntegrity <= 0)
                    destroyMe = true;
            }
        }

        /// <summary>
        /// Draw-Methode
        /// </summary>
        /// <param name="spriteBatch">Spritebacth</param>
        /// <param name="xOffset">Offset X</param>
        /// <param name="yOffset">Offset Y</param>
        new public void Draw(SpriteBatch spriteBatch, float xOffset, float yOffset)
        {
            if (destroyMe == false)
            {
                Vector2 position = new Vector2(center.X + xOffset, center.Y + yOffset);
                Vector2 origin = new Vector2(textureData.halfwidth, textureData.halfheight);
                spriteBatch.Draw(textureData.texture, position, null, Color.White, rotationAngle, origin, 1.0f, SpriteEffects.None, 0f);
            }
        }

        // Asteroid wurde getroffen
        public void Hit(int damage = 1)
        {
            structIntegrity -= damage;
        }

        #endregion Public methods

        #region Private methods

        /// <summary>
        /// Korrektur der Position, wenn das Objekt über den Spielfeldrand hinausfliegt
        /// </summary>
        private void CheckPosition()
        {
            if (center.X < 0)
            {
                center.X = worldSize + center.X;
            }
            else if (center.X >= worldSize)
            {
                center.X = center.X - worldSize;
            }

            if (center.Y < 0)
            {
                center.Y = worldSize + center.Y;
            }
            else if (center.Y >= worldSize)
            {
                center.Y = center.Y - worldSize;
            }
        }

        #endregion Private methods
    }
}
